/**
 * @description 获取当前时间对应的提示语
 * @return string
 */
export function getTimeState() {
    // 获取当前时间
    const timeNow = new Date()
    // 获取当前小时
    const hours = timeNow.getHours()
    // 判断当前时间段
    if (hours >= 6 && hours <= 10) return `早上好 ⛅`
    if (hours >= 10 && hours <= 14) return `中午好 🌞`
    if (hours >= 14 && hours <= 18) return `下午好 🌞`
    if (hours >= 18 && hours <= 24) return `晚上好 🌛`
    if (hours >= 0 && hours <= 6) return `凌晨好 🌛`
}

/**
 * @description 获取当前操作系统
 * @return string
 */
export const checkOs = () => {
    const agent = navigator.userAgent.toLowerCase();
    const isWindows = agent.indexOf("win") !== -1;
    const isMac = agent.indexOf("mac") !== -1;
    const isLinux = agent.indexOf("linux") !== -1;
    const isUnix = agent.indexOf("unix") !== -1;
    let os_type = "";
    if (isWindows) os_type = "Windows";
    else if (isMac) os_type = "Mac";
    else if (isLinux || isUnix) os_type = "Linux/Unix";
    return os_type;
}

/**
 * @description: 时间格式化
 * @param {number | string | Date} value 时间值
 * @param {string} format 格式
 * @return {string}
 */
export function dateFormat(value: number | string | Date = Date.now(), format = 'YYYY-MM-DD HH:mm:ss'): string {
  const date = typeof value === 'number' || typeof value === 'string' ? new Date(value) : value
  let showTime = format
  if (showTime.includes('SSS')) {
      const S = String(date.getMilliseconds())
      showTime = showTime.replace('SSS', S.padStart(3, '0'))
  }
  if (showTime.includes('YY')) {
      const Y = String(date.getFullYear())
      showTime = showTime.includes('YYYY') ? showTime.replace('YYYY', Y) : showTime.replace('YY', Y.slice(2, 4))
  }
  if (showTime.includes('M')) {
      const M = String(date.getMonth() + 1)
      showTime = showTime.includes('MM') ? showTime.replace('MM', M.padStart(2, '0')) : showTime.replace('M', M)
  }
  if (showTime.includes('D')) {
      const D = String(date.getDate())
      showTime = showTime.includes('DD') ? showTime.replace('DD', D.padStart(2, '0')) : showTime.replace('D', D)
  }
  if (showTime.includes('H')) {
      const H = String(date.getHours())
      showTime = showTime.includes('HH') ? showTime.replace('HH', H.padStart(2, '0')) : showTime.replace('H', H)
  }
  if (showTime.includes('m')) {
      const m = String(date.getMinutes())
      showTime = showTime.includes('mm') ? showTime.replace('mm', m.padStart(2, '0')) : showTime.replace('m', m)
  }
  if (showTime.includes('s')) {
      const s = String(date.getSeconds())
      showTime = showTime.includes('ss') ? showTime.replace('ss', s.padStart(2, '0')) : showTime.replace('s', s)
  }
  return showTime
}
